package sapling.web.admin;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sapling.api.model.ApiLandMassModel;
import sapling.dao.*;
import sapling.form.AreaForm;
import sapling.form.LandForm;
import sapling.model.*;
import sapling.service.AreaLocationService;
import sapling.service.PutOutService;
import sapling.service.TreeService;
import sapling.utils.MathUtil;
import sapling.utils.Msg;

import java.util.*;

/**
 * Created by Administrator on 2017/6/2.
 */
@Controller
@RequestMapping(value = "/admin/areaLocation")
public class AreaLocationAction {

    @Autowired
    private AreaLocationDao areaLocationDao;
    @Autowired
    private AreaLocationService areaLocationService;
    @Autowired
    private LandMassDao landMassDao;
    @Autowired
    private TreeDao treeDao;
    @Autowired
    private OutboundOrderDao outboundOrderDao;
    @Autowired
    private MarkorderDao markorderDao;
    @Autowired
    private TreeService treeService;
    @Autowired
    private BatchDao batchDao;
    @Autowired
    PutOutService putOutService;

    @RequestMapping("/addDelete")
    public String areaLocation(Model model){
        model.addAttribute("areaLocation",areaLocationDao.findByOrderBySerialNumberAsc());
        return "admin/areaLocation/ad_list";
    }

    @GetMapping("/toSmalldivide")
    public String toSmalldivide(String areaid,Model model){
        AreaLocation areaLocation = areaLocationDao.findbyname(areaid);
        model.addAttribute("area",areaLocation);
        return "admin/Smallarea";
    }
    @GetMapping("/toautogenerate")
    public String toAutoGenerate(String areaid,Model model){
        AreaLocation areaLocation = areaLocationDao.findOne(Long.parseLong(areaid));
        model.addAttribute("area",areaLocation);
        return "admin/autoGenerate";
    }
    @RequestMapping("/divide")
    public String divide(){
        return "admin/AreaCalc";
    }
    @RequestMapping("/save")
    @ResponseBody
    public void save(String areaArray,double areah,double areaw,double curveR,double carW,double curR){
        areaLocationService.save(areaArray, areah, areaw,curveR, carW,curR);
    }
    @RequestMapping("/savearea")
    @ResponseBody
    public Msg<Boolean> saveArealocation(@RequestBody List<AreaForm> areaFormList){
        areaLocationService.saveArea(areaFormList);
        return Msg.newMsg(Boolean.TRUE);
    }
    @RequestMapping("/saveland")
    @ResponseBody
    public Msg<Boolean> saveArealocation(@RequestBody LandForm landForm){
        areaLocationService.saveLand(landForm);
        return Msg.newMsg(Boolean.TRUE);
    }

    @RequestMapping("/data")
    public String data(Model model){

        List<AreaLocation> blue= areaLocationDao.findAll();
        model.addAttribute("blue",(Objects.nonNull(blue)&&blue.size()>0)?blue.size():0);

        List<LandMass> blue1=landMassDao.findAll();
        model.addAttribute("blue1",(Objects.nonNull(blue1)&&blue1.size()>0)?blue1.size():0);

        List<Tree> blue2=treeDao.findCountLandmass();
        model.addAttribute("blue2",(Objects.nonNull(blue2)&&blue2.size()>0)?blue2.size():0);

        int cyan=0;
        cyan=treeDao.findSum();
        model.addAttribute("cyan",cyan);
        model.addAttribute("cyan1",(Objects.nonNull(treeDao.findTreeGroupNotSales())&&treeDao.findTreeGroupNotSales().size()>0)
                ?treeDao.findTreeGroupNotSales().size():0);

        int cyan2=0;
        List<Tree> reg=treeDao.findByReg();
        if(Objects.nonNull(reg)&&reg.size()>0){
            cyan2=reg.size();
        }
        model.addAttribute("cyan2",cyan2);

        model.addAttribute("black",batchDao.findAll().size());
        List<Tree> treelist=treeDao.findByGROUPBybatch(4);
        List<Batch> batch=new ArrayList<>();
        for (Tree tree:treelist) {
           if(Objects.nonNull(tree.getBatch())){
               batch.add(tree.getBatch());
               break;
           }
        }
        model.addAttribute("black1",treelist.size());
        model.addAttribute("black2",treelist);

        int number=0;
        List<Tree> treeList= putOutService.put();
        if(Objects.nonNull(treeList)&&treeList.size()>0){
            number=treeList.size();
        }
        model.addAttribute("red1",number);

        int number2=0;
        List<Tree> trees=treeDao.listout(Tree.TYPE_TREE_AWAIT_OUT);
        if(Objects.nonNull(trees)&&trees.size()>0){
            number2=trees.size();
        }
        model.addAttribute("red2",number2);
        int number3=0;
        List<Tree> treesdie =treeDao.listdie(5);
        if(Objects.nonNull(treesdie)&&treesdie.size()>0){
            number3=treesdie.size();
        }
        model.addAttribute("red3",number3);
        model.addAttribute("sum",number+number2+number3);

        //treeService.sandinfo();
        return "admin/areaLocation/data";
    }

    @RequestMapping("/number")
    @ResponseBody
    public  List<String> number(){
        List<Tree> trees = treeDao.findResstate(0);
        int number=0;
        for (Tree value:trees) {
            if(0==value.getPrice()){
                number++;
            }
        }
        int number2=0;
        List<OutboundOrder> outStatus = outboundOrderDao.findByStatus(0);
        List<Tree> treesout=putOutService.treeList(outStatus,Tree.TYPE_TREE_AWAIT_OUT);
        if(Objects.nonNull(treesout)&&treesout.size()>0){
            number2=treesout.size();
        }

        int number3=0;
        List<Tree> treesdie =treeDao.listdie(5);
        if(Objects.nonNull(treesdie)&&treesdie.size()>0){
            number3=treesdie.size();
        }
        List<String> strings=new ArrayList<>();
        strings.add(number+"");
        strings.add(number2+"");
        strings.add(number3+"");
        return strings;
    }

    @RequestMapping("/add")
    public String add(AreaLocation area){
        areaLocationService.add(area);
        return "redirect:/admin/areaLocation/data";
    }

    @RequestMapping("/update")
    public String update(){
        return "admin/Areas";
    }

    @RequestMapping("/counties")
    @ResponseBody
    public List<LandMass> counties(Long id){
        List<LandMass> landMasses=new ArrayList<>();
        AreaLocation areaLocation=areaLocationDao.findOne(id);
        if(Objects.nonNull(areaLocation)){
            landMasses=areaLocation.getLandMasses();
        }
        return landMasses;
    }

    @RequestMapping("/landMasses")
    private String landMasses(Model model,Long areaLocation_id){
        List<AreaLocation> areaLocations=areaLocationDao.findAll();
        model.addAttribute("areaLocation", areaLocations);
        model.addAttribute("areaLocation_id", areaLocation_id);
        if(Objects.nonNull(areaLocation_id)){
            model.addAttribute("landMasses", areaLocationDao.findOne(areaLocation_id).getLandMasses());
        }else {
            List<LandMass> landMasses=new ArrayList<>();
            landMasses.add(new LandMass());
            model.addAttribute("landMasses", landMasses);
        }
        return "admin/areaLocation/data";
    }

    @RequestMapping("/rightdata")
    @ResponseBody
    public Map rightdata(Long landMass_id){
        Map<String,String> map=new HashedMap();
        if(Objects.nonNull(landMass_id)){
            LandMass landMass=landMassDao.findOne(landMass_id);
            map.put("name",landMass.getAreaLocation().getSerialNumber()+ landMass.getSerialNumber());
            map.put("health","0");
            map.put("abnormal","0");
            map.put("gross",landMass.getTreeWells().size()+"");
            map.put("empty","0");
            map.put("occupy","0");
        }
        return map;
    }

    @RequestMapping("/delete")
    public String delete(Long areaLocation_id){
        areaLocationService.deleteArea(areaLocation_id);
        return "redirect:/admin/areaLocation/addDelete";
    }

    @RequestMapping("/individual")
    public String individual(Model model,Long areaLocation_id){
        List<ApiLandMassModel> apilist=new ArrayList<>();
        if(Objects.nonNull(areaLocation_id)){
            AreaLocation areaLocation=areaLocationDao.findOne(areaLocation_id);
            for (LandMass value:areaLocation.getLandMasses()) {
                ApiLandMassModel api=new ApiLandMassModel();
                api.setLandMass_id(value.getLandMass_id());
                api.setSerialNumber(value.getSerialNumber());
                int treeNumber=treeDao.findTreeNumber(value.getLandMass_id());
                if(Objects.nonNull(value.getTreeWells())&&value.getTreeWells().size()>0){
                    api.setTreeWell_quantity(MathUtil.getInt(treeNumber,value.getTreeWells().size()));
                }
                List<String> lists=new ArrayList<>();
                Set<String> set=new HashSet<>();
                List<Tree> trees=treeDao.findBylandmass(value.getLandMass_id());
                for (Tree treevalue:trees) {
                    if(Objects.nonNull(treevalue.getTreeSpecies())&&treevalue.getTreeSpecies().length()>0){
                        set.add(treevalue.getTreeSpecies());
                    }
                }
                lists.addAll(set);
                api.setTreesplist(lists);
                apilist.add(api);
            }
            model.addAttribute("areaLocation",areaLocation);
            model.addAttribute("landmass",apilist);
        }
        return "admin/areaLocation/individual_list";
    }
    @Autowired
    TreeWellDao treeWellDao;
    @RequestMapping("/listArea")
    @ResponseBody
    public Msg<List<AreaLocation>> listArea(){
        List<AreaLocation> all = areaLocationDao.findAll();
        for(AreaLocation single:all){
            single.setRadius(landMassDao.getAreacount(single.getAreaLocation_id()));
            single.setRoadBreadth(areaLocationDao.getTreecount(single.getAreaLocation_id()));
            List<String> alltreename = areaLocationDao.getAlltreename(single.getAreaLocation_id());
            StringBuilder spec=new StringBuilder();
            for(String t:alltreename){
                spec.append(t+";");
            }
            if(alltreename.size()>0) {
                single.setTreespec(spec.toString().substring(0,spec.toString().length()-1));
            }else{
                single.setTreespec("");
            }
            for(LandMass landMass:single.getLandMasses()){
                if(landMass.getGenerate()==0) {
                    landMass.setPlant(-1);
                }else{
                    List<String> alltreename2 = landMassDao.getAlltreename(landMass.getLandMass_id());
                    StringBuilder sbd=new StringBuilder();
                    for(String t:alltreename2){
                        sbd.append(t+",");
                    }
                    if(sbd.length()>0)sbd=sbd.deleteCharAt(sbd.length()-1);
                    landMass.setTreeSpecies(sbd.toString());
                    landMass.setPlant(treeWellDao.getCountisUse(1,landMass.getLandMass_id()).intValue());
                    landMass.setAmount(treeWellDao.getCountlandmass(landMass.getLandMass_id()).intValue());
                }
            }
        }
        return Msg.newMsg(all);
    }

    @RequestMapping("/upname")
    @ResponseBody
    public void upname(Long id,String values){
        if(Objects.nonNull(id)){
            AreaLocation areaLocation=areaLocationDao.findOne(id);
            areaLocation.setSerialNumber(values);
            areaLocationDao.save(areaLocation);
        }

    }
}
